package com.bdc.user.service.impl;

import com.bdc.user.UserVar;
import com.bdc.user.dao.IUserDao;
import com.bdc.user.domain.UserInfo;
import com.bdc.user.service.IUserService;
import com.bdc.utils.StringUtils;
import com.google.inject.Inject;

/**
 * User: shawn
 * Date: 2011-7-5
 * Time: 23:22:59
 */
public class IUserServiceImpl implements IUserService {

    @Inject
    private IUserDao userDao;

    public long userRegister(String mail, String password) throws Exception {
        UserInfo info = userDao.getUserInfo(mail);
        if (info != null) return UserVar.REGISTER_ERROR_USER_EXIST;

        password = StringUtils.SHAString(password);
        info = new UserInfo(mail, password);
        userDao.addUser(info);
        return info.getUserId();
    }

    public long login(String mail, String password) throws Exception {
        UserInfo info = userDao.getUserInfo(mail);
        if (info == null) return UserVar.LOGIN_ERROR;

        password = StringUtils.SHAString(password);
        boolean isPasswordRight = info.getPassword().equals(password);
        if (!isPasswordRight) return UserVar.LOGIN_ERROR;

        return info.getUserId();
    }
}
